package com.rqf.mr.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IRoundService;

@Service("RoundService")
public class RoundService implements IRoundService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public Boolean insertRoundRecord(String inpatiId, String nurseLoginId){
		if ( StringUtils.isEmpty(inpatiId) || StringUtils.isEmpty(nurseLoginId) ) {
			return false;
		}
		int result =0;
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call ROUNDSRECORD_INSERT(?,?,?)}");   
			cs.setString(1, inpatiId);  
			cs.setString(2, nurseLoginId); 
			cs.registerOutParameter(3, OracleTypes.NUMBER);
			cs.execute();  
			result = cs.getInt(3);//获取单个返回值
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
		
		if ( result>0 ) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<Map<String, Object>> searchRoundRecord(String inpatiId,
			String nurseLoginId) {
		if ( StringUtils.isEmpty(inpatiId) || StringUtils.isEmpty(nurseLoginId) ) {
			return null;
		}
		String sql = "select * from roundsrecord veo "
				+ "where veo.INPATIID='" + inpatiId + "' and veo.NURSE_LOGINID='"+nurseLoginId+"' and rownum < 6 ORDER BY ROUNDS_TIME DESC";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		for (int i = 0; i < list.size(); i++) {
			Date roundsTime = (Date)list.get(i).get("ROUNDS_TIME");
			list.get(i).put("ROUNDS_TIME", time.format(roundsTime));
		}
		return list;
	}


}
